/**
 * Copyright (C) 2011-2012 Luca Li Greci
 *
 * This file is part of pojo-creator
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.pojocreator.provider.date;

import java.util.Date;

import org.apache.commons.lang.math.RandomUtils;
import org.joda.time.DateTime;

import com.pojocreator.provider.DataProvider;

/**
 * This class implements {@link DataProvider} to provide a random {@link Date} that can be in 
 * the past or in the future.
 * <p>
 * 
 * @author Luca Li Greci
 */
public class RandomDate implements DataProvider < Date > {
    private static final int RANDOM_MAX_DAYS = 10;
    private static final int RANDOM_MAX_WEEKS = 30;
    private static final int RANDOM_MAX_YEARS= 40;
    private static final int RANDOM_MAX_HOURS = 20;
    
    
    /**
     * Return the type of element managed.
     * 
     * @return The type of element managed is <code>Integer.class</code> 
     */
    @Override
    public Class < Date > type() {
        return Date.class;
    }
    
    /**
     * The number of elements returned is 1
     * 
     * @return number of elements returned is 1
     */
    @Override
    public int size() {
        return 1;
    }

    /**
     * Return a {@link Date} that can be in the past or in the future
     * 
     * @return a {@link Date} in the past or in the future
     */
    @Override
    public Date data() {
        int i = RandomUtils.nextInt();
        DateTime dt = new DateTime();
        if (i % 2 == 0) {
            dt = future(dt);
        } else {
            dt = past(dt);
        }
        return dt.toDate();
    }

    /**
     * Return a random date in the past 
     * 
     * @param dt Current {@link DateTime}
     * @return a {@link DateTime} in the past 
     */
    protected DateTime past(final DateTime dt) {
        DateTime temp = dt.minusDays(RandomUtils.nextInt(RANDOM_MAX_DAYS));
        temp = temp.minusWeeks(RandomUtils.nextInt(RANDOM_MAX_WEEKS));
        temp = temp.minusYears(RandomUtils.nextInt(RANDOM_MAX_YEARS));
        temp= temp.minusHours(RandomUtils.nextInt(RANDOM_MAX_HOURS));
        return temp;
    }

    /**
     * Return a random date in the future
     * 
     * @param dt Current {@link DateTime}
     * @return a {@link DateTime} in the future
     */
    protected DateTime future(final DateTime dt) {
        DateTime temp = dt.plusDays(RandomUtils.nextInt(RANDOM_MAX_DAYS));
        temp = temp.plusWeeks(RandomUtils.nextInt(RANDOM_MAX_WEEKS));
        temp = temp.plusYears(RandomUtils.nextInt(RANDOM_MAX_YEARS));
        temp = temp.plusHours(RandomUtils.nextInt(RANDOM_MAX_HOURS));
        return temp;
    }

}
